package com.waiter.moximoxi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.waiter.moximoxi.R;
import com.waiter.moximoxi.bean.StoreBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.yhong.aframework.adapter.ZmAdapter;
import cn.yhong.aframework.adapter.ZmHolder;
import cn.yhong.aframework.dialog.BaseDialog;
import cn.yhong.aframework.listener.OnCallbackListener;

/**
 * Created by 17639 on 2020/6/1.
 */

public class StoreChooseDialog extends BaseDialog {

    private ImageView dialog_store_choose_dis;
    private GridView dialog_store_choose_gridview;

    private StoreAdapter storeAdapter;
    private List<StoreBean> storeBeans;

    public StoreChooseDialog(Context context, OnCallbackListener onCallbackListener) {
        super(context, onCallbackListener);
    }

    @Override
    public Dialog initDialog(Context context) {
        View view = View.inflate(context, R.layout.dialog_login_sotre_choose, null);
        dialog_store_choose_dis = view.findViewById(R.id.dialog_store_choose_dis);
        dialog_store_choose_gridview = view.findViewById(R.id.dialog_store_choose_gridview);

        dialog_store_choose_dis.setOnClickListener(this);

        storeBeans = new ArrayList<>();
        storeAdapter = new StoreAdapter(context, storeBeans);
        dialog_store_choose_gridview.setAdapter(storeAdapter);

        return initMatchWrapDialog(view, context, Gravity.CENTER, android.R.style.Animation_InputMethod);
    }


    @OnClick({R.id.dialog_store_choose_dis})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_store_choose_dis:
                dismissDialog();
                break;
        }
    }


    public void showStoreDialog(final int shop_id, List<StoreBean> list) {
        storeBeans.clear();
        if (list != null && list.size() > 0) {
            if (shop_id == 0) {
                list.get(0).isSelected = true;
            } else {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).isSelected = false;
                }
                for (int i = 0; i < list.size(); i++) {
                    if (shop_id == list.get(i).shop_id) {
                        list.get(i).isSelected = true;
                    }
                }
            }

            storeBeans.addAll(list);
            storeAdapter.notifyDataSetChanged();

            showDialog();
        }
    }


    class StoreAdapter extends ZmAdapter<StoreBean> {

        public StoreAdapter(Context mContext, List<StoreBean> itemList) {
            super(mContext, itemList);
        }

        @Override
        public void init(ZmHolder zmHolder, final StoreBean storeBean, int position) {

            TextView item_store_tv = zmHolder.getView(R.id.item_store_tv);
            item_store_tv.setText(storeBean.shop_name);

            item_store_tv.setTextColor(storeBean.isSelected ? context.getResources().getColor(R.color.colorWhite) : context.getResources().getColor(R.color.colorBlack));
            item_store_tv.setBackground(storeBean.isSelected ?
                    context.getResources().getDrawable(R.drawable.shape_solid_red_40dp) : context.getResources().getDrawable(R.drawable.shape_stoke_gray_40dp));

            item_store_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCallbackListener.onCallback(0, storeBean);
                    dismissDialog();
                }
            });
        }

        @Override
        public int setLayout() {
            return R.layout.item_store_choose;
        }
    }
}
